﻿using AntdUI;
using EONFULLSIN.Common.App;
using EONFULLSIN.Languages;
using EONFULLSIN.Models.Entity.User;
using EONFULLSIN.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EONFULLSIN.Views.UserView
{
    public partial class UserLogin : UserControl
    {
        private Window window;
        private string _formTitle;

        public UserLogin(string formTitle)
        {
            _formTitle = formTitle;
            InitializeComponent();
            this.lbl_title.Text = formTitle;
            LanguageChangedEvent.LanguageChanged += UpdateLanguage;
            this.btn_login.Click += Btn_login_Click;
            this.btn_close.Click += Btn_close_Click;
            UpdateLanguage(LanguageHelper.CurrentLanguage);
        }

        private void UpdateLanguage(string obj)
        {
            if (_formTitle.Equals("用户登录"))
            {
                lbl_title.Text = LanguageHelper.GetTranslation(obj, "UserLoginTitle");
            }
            else
            {
                lbl_title.Text = LanguageHelper.GetTranslation(obj, "UserSwitchTitle");
            }
            label1.Text = LanguageHelper.GetTranslation(obj, "UserLogin_Name");
            txt_user.PlaceholderText = LanguageHelper.GetTranslation(obj, "Text_UserName");
            txt_pwd.PlaceholderText = LanguageHelper.GetTranslation(obj, "Text_Pwd");
            label2.Text = LanguageHelper.GetTranslation(obj, "UserLogin_Pwd");
            btn_login.Text = LanguageHelper.GetTranslation(obj, "Button_Login");
            btn_close.Text = LanguageHelper.GetTranslation(obj, "Button_Close");
        }

        private void Btn_close_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void Btn_login_Click(object sender, EventArgs e)
        {
            string user = txt_user.Text;
            string pwd = txt_pwd.Text;
            if (!string.IsNullOrWhiteSpace(user) && !string.IsNullOrEmpty(pwd))
            {
                var userInfo = EFDAL.Instance.GetModel<User>(x => x.Account.Equals(user) && x.PassWord.Equals(pwd));
                if (userInfo != null)
                {
                    AppInfo.IsLogin = true;
                    AppInfo.CurrentUser = userInfo;
                    this.Dispose();
                }
            }
            else
            {
                AntdUI.Message.error(window, "内容为空请检查！");
            }
        }
    }
}